import pytest
from errors import Missing
from fake_data import clients as fake_data
from service import clients as service


@pytest.fixture
def client_json():
    return {
        "name": "Ivan",
        "surname": "Ivanov",
        "credit_card": "1234123412341234",
        "car_number": "A000AA98",
    }


@pytest.fixture
def patched_data(monkeypatch):
    monkeypatch.setattr(
        service.data, "create_client_json", fake_data.create_client_json
    )
    monkeypatch.setattr(service.data, "get_client_json", fake_data.get_client_json)
    monkeypatch.setattr(
        service.data, "get_all_clients_json", fake_data.get_all_clients_json
    )
    yield


def test_create_client(client_json, patched_data):
    result = service.create_client(**client_json)
    # We use id=100 to be sure thet we have run fake `create_client_json()`:
    assert result == {"id": 100, **client_json}


def test_get_client(client_json, patched_data):
    result = service.get_client(client_id=100)
    assert result == {"id": 100, **client_json}


def test_get_client_with_wrong_id(client_json, patched_data):
    with pytest.raises(Missing):
        service.get_client(client_id=123)


def test_get_all_clients(client_json, patched_data):
    result = service.get_all_clients()
    assert result == [{"id": 100, **client_json}]
